var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsinfo:[],
    number:1,
    total_money:0,
    goods_id:0,
    defaultAddr:[],
    addressId:0,
    img_url: c.img_url,
  },
  onLoad: function (options) {
      this.setData({
          goods_id:options.id
      });
  },
  onShow:function(){
      this.getDefaultAddr();
      this.getGoods();
  },
  //数量减
  clickdown:function(){
    if (this.data.number==1){
      wx.showToast({
        title: '数量不能小于1',
        icon: 'none',
        duration: 2000
      })
      this.setData({
        number:1,
        total_money:this.data.goodsinfo.price
      })
    }else{
      var num = this.data.number - 1;
      var total_money = this.data.goodsinfo.price*num;
      this.setData({
        number: num,
        total_money:total_money.toFixed(2)
      })
    }
  },
  //产品详情
   getGoods:function(){
        var that = this;
        var data = {
            url: "/v1/goodspoint/detail",
            goods_id: this.data.goods_id,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                var total_money = data.price*that.data.number;
                that.setData({
                    goodsinfo: data,
                    total_money:total_money.toFixed(2)
                });
            }
        });
    },

  getDefaultAddr:function(){//获取默认地址
        var that = this
        var data = {
            url: "/v1/Useraddress/getdefault",
            token: wx.getStorageSync("userinfo").wap_token,
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                if (data.id == undefined){
                    that.setData({
                        defaultAddr: data,
                        addressId: 0
                    });
                }else{
                    that.setData({
                        defaultAddr: data,
                        addressId: data.id
                    });
                }
            } else {

            }
        });
    },
    //提交订单
    submit:function(){
        if (this.data.addressId == 0 ){
            utils.errMsg("请先选择一个地址");
            return false;
        };
        var data = {
            url: "/v1/ordergoodspoint/add",
            token: wx.getStorageSync("userinfo").wap_token,
            goods_id:this.data.goods_id,
            nums:this.data.number,
            address_id:this.data.addressId,
            note:''
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                utils.sucMsg('兑换成功');
                  setTimeout(function () {
                      wx.redirectTo({
                          url: '/pages/my/integral/index?select=1',
                      })
                  }, 2000);
                  return false;
            }else{
              utils.errMsg(res.msg);
            }
        });
    },
  clickup:function(){
    var num=this.data.number+1;
    var total_money = this.data.goodsinfo.price*num;
    this.setData({
      number:num,
      total_money:total_money.toFixed(2)
    });
  }
 
})